package com.base.cn.platform.os.controller.classes.topic;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.classes.TeacherDataIsolation;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.course.classes.topic.ClassesTopicService;
import com.base.cn.platform.os.service.course.classes.topic.type.ClassesTopicTypeService;
import com.base.cn.platform.os.service.manage.teacher.SysTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 班级话题
 *
 * @author SK
 * @since 2018-05-18
 */
@Controller
@RequestMapping("/admin/classes/topic")
public class AdminClassesTopicController extends AdminBaseController {

    @Autowired
    private ClassesTopicService classesTopicService;
    @Autowired
    private SysTeacherService sysTeacherService;
    @Autowired
    private ClassesTopicTypeService classesTopicTypeService;

    /**
     * 初始化修改或添加班级话题页面
     *
     * @param id 班级话题id
     * @return 修改或添加班级话题页面
     */
    @RequestMapping({"/initUpdate", "/initAdd"})
    public String initUpdateOrAdd(Model model,
                                  @RequestParam(value = "id", required = false) BigDecimal id
                                 ,@RequestParam(value = "crumbs", required = false) String crumbs) {
        Map<String, Object> topic = null;
        if (DataUtil.idIsNotNull(id)) {
            topic = classesTopicService.findClassesTopicById(id, false, true, false, false);
        }
        if (topic == null) {
            topic = Collections.singletonMap("status", 0);
        }
        List<Map<String, Object>> classesTopicType = classesTopicTypeService.getClassesTopicType(new BigDecimal(0));
        model.addAttribute("classifies", classesTopicType);
        model.addAttribute("topic", topic);
        if (StringUtils.isNotEmpty(crumbs) && crumbs.equals("true")) {
            model.addAttribute("crumbs",crumbs);
            return resultPage("/classes/topic/save-report-topic");
        }
        return resultPage("/classes/topic/save-topic");
    }

    /**
     * 初始化班级话题列表页面
     *
     * @return 班级话题列表页面
     */
    @RequestMapping("/topicList")
    public String topicList(Model model) {
        Map<Integer, Object> classifies = classesTopicService.classesTopicClassify();
        model.addAttribute("classifies", classifies);
        return resultPage("/classes/topic/topic-list");
    }

    /**
     * 班级话题列表页面
     *
     * @return 班级话题列表页面
     */
    @RequestMapping("/ajaxTopicListPage")
    @TeacherDataIsolation(type = 2)
    public String ajaxTopicListPage(Model model,
                                    @RequestBody Map<String, Object> topic,
                                    @RequestParam("currentPage") int currentPage) {
        Map<String, Object> dataMap = classesTopicService.findClassesTopicListPage(topic, currentPage, null, null, true, true, true);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/classes/topic/ajax-topic-list");
    }

    /**
     * 保存班级话题
     *
     * @param topic 班级话题
     * @return 保存结果
     */
    @RequestMapping("/saveTopic")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "保存班级话题")
    public Map<String, Object> saveTopic(HttpServletRequest request,
                                         @RequestBody Map<String, Object> topic) {
        BigDecimal createUserId = loginUtil.getLoginUserId(request);
        //后台创建话题时区分是后台管理员创建还是教师创建
        Map map = new HashMap();
        map.put("sysUserId", createUserId);
        List sysTeacherList = sysTeacherService.findSysTeacherList(map, false, false, false, false, false);
        topic.put("createUserId", createUserId);
        if (ObjectUtils.isEmpty(sysTeacherList)) {
            topic.put("source", 2);
        } else {
            Map<String, Object> sysTeacher = (Map<String, Object>) sysTeacherList.get(0);
            topic.put("createUserId", sysTeacher.get("id"));
            topic.put("source", 3);
        }
        String classesIds = (String) topic.remove("classesIds");
        return classesTopicService.saveClassesTopic(topic, classesIds);
    }

    /**
     * 更新班级话题置顶
     *
     * @param ids 班级作业id
     * @param top 班级作业状态 1.置顶 2.取消置顶
     * @return 操作结果
     */
    @RequestMapping("/updateTopicTop")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "更新班级话题置顶")
    public Map<String, Object> updateTopicTop(@RequestParam("ids") String ids,
                                              @RequestParam("top") int top) {
        ids = DataUtil.filterNotNumberStr(ids);
        if (StringUtils.isEmpty(ids)) {
            return ResultUtil.ERROR("选择操作的班级话题");
        }
        return classesTopicService.updateClassesTopicTop(ids, top);
    }

    /**
     * 更新班级话题状态
     *
     * @param ids    班级作业id
     * @param status 班级作业状态 0.未发布 1.已发布 2.已禁用
     * @return 操作结果
     */
    @RequestMapping("/updateTopicStatus")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "更新班级话题状态")
    public Map<String, Object> updateTopicStatus(@RequestParam("ids") String ids,
                                                 @RequestParam("status") int status) {
        ids = DataUtil.filterNotNumberStr(ids);
        if (StringUtils.isEmpty(ids)) {
            return ResultUtil.ERROR("选择操作的班级话题");
        }
        return classesTopicService.updateClassesTopicStatus(ids, status);
    }

    /**
     * 查看话题详情
     *
     * @param model
     * @param id     话题id
     * @param crumbs 是否加载没有面包屑的话题详情页面（话题举报-查看话题详情）
     * @return
     */
    @RequestMapping("/previewTopic")
    public String previewTopic(Model model,
                               @RequestParam("id") BigDecimal id, @RequestParam(value = "crumbs", required = false) String crumbs) {
        Map<String, Object> topic = classesTopicService.findClassesTopicById(id, true, true, true, false);

        model.addAttribute("topic", topic);
        if (StringUtils.isNotEmpty(crumbs) && crumbs.equals("true")) {
            return resultPage("/classes/topic/preview-report-topic");
        }
        return resultPage("/classes/topic/preview-topic");
    }

    /**
     * 话题列表只查看本班级的
     *
     * @param model
     * @param topic
     * @param currentPage
     * @return
     */
    @RequestMapping("/ajaxClassesTopicListPage")
    @TeacherDataIsolation(type = 2)
    public String ajaxClassesTopicListPage(Model model,
                                           @RequestBody Map<String, Object> topic,
                                           @RequestParam("currentPage") int currentPage) {
        Map<String, Object> dataMap = classesTopicService.findClassesTopicListPage(topic, currentPage, null, null, true, true, true);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/classes/topic/ajax-classes-topic-list");
    }

}
